//
//  ViewController.m
//  UI_20_homework_XML数据解析
//
//  Created by li cheng on 16/8/10.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "AlbumInfo.h"
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *albumInfoArray;
@property (nonatomic, copy) NSString *currentElementName;
@property (nonatomic, strong) NSMutableArray *domAlbumInfoArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}
- (IBAction)SAXDataHandle:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    // 创建xml解析器
    NSXMLParser *xmlparsere = [[NSXMLParser alloc] initWithData:data];
    // 指定代理人
    xmlparsere.delegate = self;
    // 开始解析
    [xmlparsere parse];
}
#pragma mark - 解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"%@", parser);
    _albumInfoArray = [NSMutableArray array];
}
#pragma mark - 遇到节点头,方法回调, 记录当前节点,方便以后赋值
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"AlbumInfo"]) {
        AlbumInfo *albumInfo = [[AlbumInfo alloc] init];
        [_albumInfoArray addObject:albumInfo];
    }
}
#pragma mark - 节点的值,根据节点来决定值,没有值得啥时候对应 \n\t \n\t\t
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if (_currentElementName) {
        AlbumInfo *albumInfo = [_albumInfoArray lastObject];
        [albumInfo setValue:string forKey:_currentElementName];
    }
}
#pragma mark - 遇到节点尾, 方法回调和当前的节点结束
- (void)parser:(NSXMLParser *)parser didEndElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{
    _currentElementName = nil;
}
#pragma mark - 解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"%@", _albumInfoArray);
   
}
#pragma mark - SAX-date数据解析 Button 数据解析方法
- (IBAction)DOMDataHandle:(id)sender {
    _domAlbumInfoArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    GDataXMLDocument *documnet = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
    GDataXMLElement *rootElement = [documnet rootElement];
    NSArray *rootChildElements = rootElement.children;
    NSLog(@"________________%@", rootElement.name);
    for (GDataXMLElement *childElement in rootChildElements) {
  NSLog(@"============================================================================================================================================================================================================================================================================================================================================================================================================childElement--name:%@", childElement.name);
        AlbumInfo *albumInfo = [AlbumInfo new];
        [self.albumInfoArray addObject:albumInfo];
        for (GDataXMLElement *grandSonElment in childElement.children) {
            [albumInfo setValue:grandSonElment.stringValue forKey:grandSonElment.name];
        }
    }
    NSLog(@"%@", self.domAlbumInfoArray);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
